﻿using System.Collections.Generic;
using System.Data.Common;

namespace DataAccess
{
    /// <summary>
    /// A database command to retrieve a enumeration of objects
    /// </summary>
    public class CollectionDatabaseCommand<T> : QueryDatabaseCommand<T>
    {
        #region Properties

        /// <summary>
        /// The enumeration of objects retrieved from the database
        /// </summary>
        public IList<T> Objects { get; private set; }

        #endregion
 
        public override int Read(DbDataReader reader)
        {
            Objects = new List<T>(); // Make sure we have an empty list in all the cases

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    object obj = ReadObject(reader);
                    Objects.Add((T)obj);
                }

                return Objects.Count;
            }

            return 0;
        }
    }
}
